package cn.keniykey.mc.landterritory.landflag.flag;

import java.util.Arrays;

import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.FoodLevelChangeEvent;

import cn.keniykey.mc.landterritory.config.ConfigurationManager;
import cn.keniykey.mc.landterritory.event.LandTerritoryReloadConfigEvent;
import cn.keniykey.mc.landterritory.land.api.ILandManager;
import cn.keniykey.mc.landterritory.land.api.Land;
import cn.keniykey.mc.landterritory.land.api.LandUtil;
import cn.keniykey.mc.landterritory.land.api.LandUtil.ELandPermissionLevel;
import cn.keniykey.mc.landterritory.landflag.api.ELandFlagParType;
import cn.keniykey.mc.landterritory.landflag.api.ELandFlagType;
import cn.keniykey.mc.landterritory.landflag.api.ILandFlagDetection;
import cn.keniykey.mc.landterritory.landflag.api.JavaLandFlag;
import cn.keniykey.mc.landterritory.landflag.api.LandFlagConfigurationFile;
import cn.keniykey.mc.landterritory.landflag.api.LandFlagListenerManager;
import cn.keniykey.mc.landterritory.landflag.api.LandFlagSetting;
import cn.keniykey.mc.landterritory.toolkit.Util;
import cn.keniykey.mc.landterritory.vault.Perm;

public final class BanSatietyFlag extends JavaLandFlag implements Listener,ILandFlagDetection{
	private String per_add;
	private ELandFlagType type;
	
	@Override
	public ELandFlagParType getELandFlagParType() {
		return ELandFlagParType.NONE;
	}
	@Override
	public ELandFlagType getELandFlagType() {
		return this.type;
	}
	@Override
	public boolean allowAllPlayerData() {
		return false;
	}
	
	@Override
	public void onEnable() {
		LandFlagListenerManager.registerListener(this, this);
		LandFlagSetting.setAddDetection(this, this);
	}
	@EventHandler
	public void onConfigLoad(final LandTerritoryReloadConfigEvent e) {
		final LandFlagConfigurationFile config = this.getLandFlagConfigurationFile();
		if(!config.has()) {
			config.set("name", "不消耗饱食度");
			config.set("lore", Arrays.asList("&7（领地内的玩家将不会消耗饱食度）"));
			config.set("per", ConfigurationManager.AdminPer);
		}
		this.setDisplayName(Util.conver(config.getString("name")));
		this.setIntroductionLore(Util.conver(config.getStringList("lore")));
		this.per_add = config.getString("per");
		if(this.per_add == null || this.per_add.isEmpty()) {
			this.type = ELandFlagType.PLAYER;
		}else if(this.per_add.equals(ConfigurationManager.AdminPer)) {
			this.type = ELandFlagType.ADMIN;
		}else {
			this.type = ELandFlagType.SPECIAL;
		}
	}
	
	@EventHandler(priority = EventPriority.LOW,ignoreCancelled = true)
	public void onFoodLevelChange(final FoodLevelChangeEvent e) {
		if(!(e.getEntity() instanceof Player)) return;
		final Player player = (Player) e.getEntity();
		if(e.getFoodLevel() >= player.getFoodLevel()) return;
		final Land land = ILandManager.getILandManager().getCircleDetectionLand(player);
		if(land != null && JavaLandFlag.hasLandFlag(land, this)) e.setCancelled(true);
	}
	@Override
	public boolean onRequest(Player player, Land land) {
		switch(this.type) {
		case PLAYER:
			return true;
		case ADMIN:
			return LandUtil.hasLandPermission(player, land, ELandPermissionLevel.ADMIN);
		default:
		}
		return Perm.getPerm().hasPermission(land.getWorldName(), player, this.per_add);
	}
}